<?php
/*-----------------------------------------------------
    NewsLister view.php
	
    @package NewsLister
	@since 1.0
	
	@version    1.0.0
	@copyright Tenderfeel
	@author Tenderfeel(tenderfeel@gmail.com)
	@link http://tenderfeel.xsrv.jp/ 
------------------------------------------------------*/

define( "ABSDIR", dirname( __FILE__ ) . DIRECTORY_SEPARATOR);
require_once(ABSDIR."/include/config.php");
require_once(ABSDIR."/include/post.php");

/**
 * 新着表示
 * @param $max (num) 最大表示件数
 * @param $toenc (str) エンコード
 */
function get_newslister( $max = DISPMAX, $toenc = null)
{
	$logdata = load_data();
	$logcount= count($logdata);
	
	print "<dl>\n";
	
	for($i=0; $i < $max ; $i++){
		
		if($i > ($logcount-1)) break;
		
		if($toenc!=null){
			$logdata[$i]["date"] = to_enc($logdata[$i]["date"],$toenc);
			$logdata[$i]["comment"] =  to_enc($logdata[$i]["comment"],$toenc);
		}
		
		print "<dt>".date(DATEFORMAT,$logdata[$i]["date"])."</dt><dd>".$logdata[$i]["comment"]."</dd>\n";
		
	}

	print "</dl>\n";
}

/**
 * 全件表示
 * @param $max (num) 最大表示件数
 * @param $style (str) 表示スタイル（table, ul, null = dd)
 * @param $toenc (str) エンコード
 */
function get_newslister_all( $max = DISPMAX, $style = null, $toenc = null)
{
	
	if(isset($_GET["page"]) && is_numeric($_GET["page"])) {
		$page = htmlentities($_GET["page"]);
	}else{
		$page=0;
	}
	
	$logdata = load_data();
	$logcount= count($logdata);
	$count = count_maxpage($max, $logcount);
	$output = disp_data($logdata,$page, $max, $count);
	$paged = paged($page, $max, $count);
	
	if($toenc!=null){
		
		$logdata[$i]["comment"] =  mb_convert_encoding($logdata[$i]["comment"],$toenc,"UTF-8");
	}
	
	switch($style){
		
		case "table":
			print "<table id=\"newslister\">\n";
			foreach($output as $out){
				$date = ($toenc!=null)? date(DATEFORMAT, to_enc($out['date'],$toenc)):date(DATEFORMAT, $out['date']);
				$comment = ($toenc!=null)? to_enc($out['comment'],$toenc):$out['comment'];
				print "<tr>\n";
				print "<th scope=\"row\" class=\"date\">".$date."</th>\n";
				print "<td>".$comment."</td></tr>\n";
			}
			print "</table>";
		break;
		
		case "ul":
			print "<ul id=\"newslister\">\n";
			foreach($output as $out){
				$date = ($toenc!=null)? date(DATEFORMAT, to_enc($out['date'],$toenc)):date(DATEFORMAT, $out['date']);
				$comment = ($toenc!=null)? to_enc($out['comment'],$toenc):$out['comment'];
				
				print "<li><em class=\"date\">".$date."</em>\n";
				print $comment."</li>\n";
			}
			print "</ul>";
		break;
		
		default:
			print "<dl id=\"newslister\">\n";
			foreach($output as $out){
				$date = ($toenc!=null)? date(DATEFORMAT, to_enc($out['date'],$toenc)):date(DATEFORMAT, $out['date']);
				$comment = ($toenc!=null)? to_enc($out['comment'],$toenc):$out['comment'];
				print "<dt class=\"date\">".$date."</dt>\n";
				print "<dd>".$comment."</dd>\n";
			}
			print "</dl>";
			
	}
	
	print page_link($paged);
	
}

function to_enc($str, $toenc)
{
	return mb_convert_encoding($str,$toenc,"UTF-8");
}


function page_link($paged)
{
	//ページ番号状態を取得	
	if($paged['max']!=0){
		
		$links ="<ul class=\"pagelink\">";
		
		if(isset($paged["previous"])) {
			$links.= '<li class="prev"><a href="'.basename($_SERVER["SCRIPT_FILENAME"]).'?page='.$paged['previous'].'">&laquo; 前ページへ</a></li>';
		}
		
		if($paged['max']>=2){
			for($i=0;$i<=$paged['max'];$i++){
				$class = ($paged['current'] == $i) ? 'class="current"':"";
				$links.= '<li><a href="'.basename($_SERVER["SCRIPT_FILENAME"]).'?page='.$i.'"'.$class.'>'.($i+1).'</a></li>';
			}
		}
		
		if($paged["next"]){
			$links.= '<li class="next"><a href="'.basename($_SERVER["SCRIPT_FILENAME"]).'?page='.$paged['next'].'">次ページへ &raquo;</a></li>';
		} 
		
		$links .= "</ul>";
		
	}else{
		
		$links = "";
	}
	
	return $links;
}

?>